package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.pojo.NatatoriumListPageEntity;
import com.ruffian.library.widget.RImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/8/28.
 */

public class ADA_AppointmentList extends RecyclerView.Adapter<ADA_AppointmentList.AppointmentViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<NatatoriumListPageEntity.ListBean> mList;
    private OnBtnClickListener mListener;
    private OnItemClickListener mItemListener;
    private List<View> headerViewList;
    private final int HEAD = 1;

    public ADA_AppointmentList(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
        headerViewList = new ArrayList<>();
    }

    public void addHeader(int viewResourceId) {
        headerViewList.add(LayoutInflater.from(mContext).inflate(viewResourceId, null));
    }
    public void addHeader(View view)
    {
        headerViewList.add(view);
    }

    //添加数据
    public void addList(List<NatatoriumListPageEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    //更新数据
    public void updateList(List<NatatoriumListPageEntity.ListBean> list) {
        mList.addAll((mList.size() - 1), list);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        final int headers = headerViewList.size();
        if (position<headers)
            return HEAD;
        return super.getItemViewType(position);
    }

    @Override
    public AppointmentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD && headerViewList.size()>0)
            return new AppointmentViewHolder(headerViewList.get(0),viewType);
        return new AppointmentViewHolder(mInflater.inflate(R.layout.item_appintment_recycler_view_new, parent, false),viewType);
    }

    @Override
    public void onBindViewHolder(AppointmentViewHolder holder, final int position) {
        if (getItemViewType(position)==HEAD)
            return;
        final NatatoriumListPageEntity.ListBean entity = mList.get(position);
        Glide.with(mContext)
                .load(entity.getLogoUrl())
                .error(R.mipmap.ic_serve_default)
                .fallback(R.mipmap.ic_serve_default)
                .transform(new GlideRoundTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .into(holder.itemIvImgAppintment);
        holder.itemTvNameAppintment.setText(entity.getName());
        holder.itemTvDetails.setText(entity.getAddress());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemListener.onItemClick(entity.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AppointmentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_iv_img_appintment)
        RImageView itemIvImgAppintment;
        @BindView(R.id.item_tv_name_appintment)
        TextView itemTvNameAppintment;
        @BindView(R.id.item_tv_details)
        TextView itemTvDetails;
        @BindView(R.id.item_tv_distance)
        TextView itemTvDistance;


        public AppointmentViewHolder(View itemView,int viewType) {
            super(itemView);
            if (viewType==HEAD)
                return;
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnBtnClickListener {
        void onBtnClick(String shopId);
    }

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(String cardId);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemListener = listener;
    }


}
